package main.java.dao.impl;

import main.java.dao.TaskDao;
import main.java.domain.Task;
import main.java.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class TaskDaoImpl implements TaskDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Task> findAll(int id) {
        String sql = "SELECT * from task";

        return template.query(sql, new BeanPropertyRowMapper<Task>(Task.class));
    }

    @Override
    public List<Task> findTeacherAll(int id) {
        String sql = "select * from task where ? = tid";

        return template.query(sql, new BeanPropertyRowMapper<Task>(Task.class), id);
    }

    @Override
    public Task find(int id) {
        String sql = "select * from task where ? = id";

        return template.queryForObject(sql, new BeanPropertyRowMapper<Task>(Task.class), id);
    }

    @Override
    public List<Task> findTaskFileList(int id) {
        String sql = "select * from task where ? = id";

        return template.query(sql, new BeanPropertyRowMapper<Task>(Task.class), id);
    }

    @Override
    public boolean add(String name, String stime, String etime, int id, String path) {
        String sql = "insert into task(name, stime, etime, tid, filepath) " +
                "values('"+name+"', '"+stime+"', '"+etime+"', "+id+", '"+path+"')";

        int update = template.update(sql);

        return update > 0;
    }
}
